package com.glela.controller.init;

import java.util.Map;

import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.glela.cache.redis.RedisKeys;
import com.glela.common.constant.BaseConstants;
import com.glela.common.constant.BaseRestrictionsEnum;
import com.glela.controller.ReportBaseController;
import com.glela.platform.model.BaseRestrictions;
import com.glela.platform.service.BaseRestrictionsService;
import com.glela.report.mapper.ReportOrderMapper;
import com.glela.report.model.CacheDay;

@Controller
@RequestMapping(value = "/report/init")
public class InitReportController extends ReportBaseController {
	@Autowired
	private BaseRestrictionsService	baseRestrictionsService;
	@Autowired
	private ReportOrderMapper		reportOrderMapper;

	/** 初始化日报缓存 */
	@RequestMapping(value = "/daily", method = RequestMethod.POST)
	public @ResponseBody String init(@RequestParam Map<String, String> map) {
		try {
			DateTime startDate = null;
			DateTime nowTime = new DateTime();

			String initDate = MapUtils.getString(map, "initDate");//初始化日期
			String skin79GoodsProductIds = MapUtils.getString(map, "skin79GoodsProductIds");//skin79专属商品

			if (StringUtils.isBlank(initDate)) {
				startDate = nowTime.minusDays(1);
			} else {
				startDate = DateTime.parse(initDate, DATE_TIME_YYYY_MM_DD_FORMATTER);
				if (nowTime.getMillis() < startDate.getMillis()) {
					startDate = nowTime.minusDays(1);
				}
			}

			DateTime tomorrow = startDate.plusDays(1);//明天
			DateTime yesterday = startDate.minusDays(1);//昨天

			if (StringUtils.isBlank(skin79GoodsProductIds)) {
				BaseRestrictions baseRestrictionsForCache = baseRestrictionsService.getBaseRestrictionsForCache(BaseRestrictionsEnum.GOODS_SKIN79);
				if (baseRestrictionsForCache == null) {
					skin79GoodsProductIds = "";
				} else {
					skin79GoodsProductIds = baseRestrictionsForCache.getName();
				}
			}
			String[] goodsSkin79ProductIds = StringUtils.split(skin79GoodsProductIds, "|");

			//昨日数据
			CacheDay cacheDayyesterday = JSONObject.parseObject(getRedisUtil().hget(RedisKeys.REPORT_DAILY + yesterday.toString(BaseConstants.DATE_FORMAT_YYYYMM), yesterday.toString(BaseConstants.DATE_FORMAT_YYYYMMDD)), CacheDay.class);
			if (cacheDayyesterday == null) {
				cacheDayyesterday = new CacheDay();
			}

			CacheDay nowCacheDay = new CacheDay();
			//缓存日新增店主数
			Long storeNewNum = reportOrderMapper.selectStoreNums(startDate.toDate(), tomorrow.toDate());
			if (storeNewNum == null) {
				storeNewNum = 0l;
			}
			nowCacheDay.setStoreNewNums(storeNewNum);
			//缓存月累计开店数
			nowCacheDay.setStoreAccNums(nowCacheDay.getStoreNewNums());
			boolean avlSumYesterDay = cacheDayyesterday != null && yesterday.getMonthOfYear() == startDate.getMonthOfYear();//是否可以累加昨天数据
			if (avlSumYesterDay) {
				nowCacheDay.setStoreAccNums(nowCacheDay.getStoreAccNums() + cacheDayyesterday.getStoreAccNums());
			}

			//缓存专属SKIN79日销售额
			if (ArrayUtils.isNotEmpty(goodsSkin79ProductIds)) {
				Long salesAmountDaySkin79 = reportOrderMapper.selectSalesAmountDay(startDate.toDate(), tomorrow.toDate(), goodsSkin79ProductIds);
				if (salesAmountDaySkin79 == null) {
					salesAmountDaySkin79 = 0l;
				}
				nowCacheDay.setSalesAmountDaySkin79(salesAmountDaySkin79);
			}
			//缓存专属SKIN79月累计销售额
			nowCacheDay.setSalesAmountMonthSkin79(nowCacheDay.getSalesAmountDaySkin79());
			if (avlSumYesterDay) {
				nowCacheDay.setSalesAmountMonthSkin79(nowCacheDay.getSalesAmountMonthSkin79() + cacheDayyesterday.getSalesAmountMonthSkin79());
			}
			//缓存非专属SKIN79日销售额
			Long salesAmountDayNoSelf = reportOrderMapper.selectSalesAmountDayNoself(startDate.toDate(), tomorrow.toDate(), goodsSkin79ProductIds);
			if (salesAmountDayNoSelf == null) {
				salesAmountDayNoSelf = 0l;
			}
			nowCacheDay.setSalesAmountDayNoSelf(salesAmountDayNoSelf);
			//缓存非专属SKIN79月累计销售额
			if (avlSumYesterDay) {
				nowCacheDay.setSalesAmountMonthNoSelf(nowCacheDay.getSalesAmountDayNoSelf() + cacheDayyesterday.getSalesAmountMonthNoSelf());
			} else {
				nowCacheDay.setSalesAmountMonthNoSelf(salesAmountDayNoSelf);
			}
			//缓存skin79日订单数
			Long orderDaySkin79Nums = reportOrderMapper.selectOrderSkin79Nums(startDate.toDate(), tomorrow.toDate(), goodsSkin79ProductIds);
			if (orderDaySkin79Nums == null) {
				orderDaySkin79Nums = 0l;
			}
			nowCacheDay.setOrderDaySkin79Nums(orderDaySkin79Nums);
			//缓存skin79月订单累计数
			if (avlSumYesterDay) {
				nowCacheDay.setOrderMonthSkin79Nums(nowCacheDay.getOrderDaySkin79Nums() + cacheDayyesterday.getOrderMonthSkin79Nums());
			} else {
				nowCacheDay.setOrderMonthSkin79Nums(orderDaySkin79Nums);
			}
			//缓存skin79日交易店铺数
			Long storeDayPaySkin79Nums = reportOrderMapper.selectPayStoreSkin79Nums(startDate.toDate(), tomorrow.toDate(), goodsSkin79ProductIds);
			if (storeDayPaySkin79Nums == null || storeDayPaySkin79Nums < 0) {
				storeDayPaySkin79Nums = 0l;
			}
			nowCacheDay.setStoreDayPaySkin79Nums(storeDayPaySkin79Nums);

			//缓存日销售额
			Long salesAmountDay = reportOrderMapper.selectSalesAmountDay(startDate.toDate(), tomorrow.toDate(), null);
			if (salesAmountDay == null) {
				salesAmountDay = 0l;
			}
			nowCacheDay.setSalesAmountDay(salesAmountDay);
			//缓存月累计销售额
			nowCacheDay.setSalesAmountMonth(salesAmountDay);
			if (avlSumYesterDay) {
				nowCacheDay.setSalesAmountMonth(nowCacheDay.getSalesAmountMonth() + cacheDayyesterday.getSalesAmountMonth());
			}
			//缓存日订单量
			Long orderDayNums = reportOrderMapper.selectOrderNums(startDate.toDate(), tomorrow.toDate());
			if (orderDayNums == null || orderDayNums < 0) {
				orderDayNums = 0l;
			}
			nowCacheDay.setOrderDayNums(orderDayNums);
			//缓存月累计订单量
			nowCacheDay.setOrderMonthNums(orderDayNums);
			if (avlSumYesterDay) {
				nowCacheDay.setOrderMonthNums(nowCacheDay.getOrderMonthNums() + cacheDayyesterday.getOrderMonthNums());
			}

			//缓存日营业利润
			//缓存日交易店铺
			Long storeDayPayNums = reportOrderMapper.selectPayStoreNums(startDate.toDate(), tomorrow.toDate());
			if (storeDayPayNums == null || storeDayPayNums < 0) {
				storeDayPayNums = 0l;
			}
			nowCacheDay.setStoreDayPayNums(storeDayPayNums);

			//全部店铺数
			Long selectStoreTotalNums = reportOrderMapper.selectStoreTotalNums(tomorrow.toDate());
			if (selectStoreTotalNums == null) {
				selectStoreTotalNums = 0l;
			}
			nowCacheDay.setStoreNums(selectStoreTotalNums);
			//缓存数据
			getRedisUtil().hmset(RedisKeys.REPORT_DAILY + startDate.toString(BaseConstants.DATE_FORMAT_YYYYMM), startDate.toString(BaseConstants.DATE_FORMAT_YYYYMMDD), JSONObject.toJSONString(nowCacheDay));
			return result("成功");
		} catch (Exception e) {
			getLogger().error("报表：初始化日报信息失败：{}", e.getMessage(), e);
			return resultError(e.getMessage());
		}
	}
}